using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CarsTitans
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region Constants
        private const int SCREEN_WIDTH = 1024;
        private const int SCREEN_HEIGHT = 768;
        private const bool SCREEN_FULLSCREEN = false;
        private const float CAMERA_ZNEAR = 1.0f;
        private const float CAMERA_ZFAR = 8000.0f;
        #endregion

        #region Attributes
        GraphicsDeviceManager graphics;
        Car car = null;
        Entity floor = null;
        ThirdPersonCamera camera = null;
        Vector3 cameraEye = new Vector3(.0f,.0f,.0f);
        #endregion

        #region Methods: Init/Deinit
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = SCREEN_WIDTH;
            graphics.PreferredBackBufferHeight = SCREEN_HEIGHT;
            graphics.IsFullScreen = SCREEN_FULLSCREEN;
            graphics.ApplyChanges();

            // Setup the camera.
            camera = new ThirdPersonCamera();
            camera.Perspective( MathHelper.ToDegrees(MathHelper.PiOver4), 
                               (float)SCREEN_WIDTH/ (float)SCREEN_HEIGHT, CAMERA_ZNEAR, CAMERA_ZFAR);
            camera.LookAt(new Vector3(0.0f,200.0f,800.0f), Vector3.Zero, Vector3.Up);

            base.Initialize();
        }
        protected override void LoadContent()
        {
            car = new Car("Models/Car",Content);
            floor = new Entity("Models/Ground", Content);
        }

        protected override void UnloadContent()
        {
            Content.Unload();
        }
        #endregion
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape) )
                this.Exit();

            Vector3 dst = car.Transform.Translation + car.Transform.Backward * 800.0f + new Vector3(0.0f, 200.0f, 0.0f);
            cameraEye += (dst - cameraEye) * (float)gameTime.ElapsedGameTime.TotalSeconds * 2.0f;
            camera.LookAt(cameraEye,car.Transform.Translation,car.Transform.Up);
            camera.Update(gameTime);
            car.Update(gameTime);
            floor.Update(gameTime);
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            floor.Render(camera.ViewMatrix, camera.ProjectionMatrix);
            car.Render(camera.ViewMatrix, camera.ProjectionMatrix);
            base.Draw(gameTime);
        }
    }
}
